<?php
/**
 * 系统节点表模型
 * 
 * @author:  will.lee <im.will.lee@gmail.com>
 * @version: 1.0
 */
class NodeModel extends AppModel
{
    // 验证规则
    protected $_validate_list = array
    (
        // 添加时验证
        'add_root' => array
        (
            array('name','require','节点名称必须填写!',1),
            array('status',array(1,2),'显示状态不正确!',1,'in'),
        ),
        'add'      => array
        (
            array('name','require','节点名称必须填写!',1),
            array('g','require','所属分组必须填写!',1),
            array('m','require','模块名称必须填写!',1),
            array('a','require','操作名称必须填写!',1),
            array('status',array(1,2),'显示状态不正确!',1,'in'),
        ),
    );
    
    /**
     * 管理端菜单初始化
     */
    public function initAdminMenuJson()
    {
        $login_info = session(C('ADMIN_SESSION_KEY'));
        $menu_json  = array();
        // 取得ROOT节点
        $where      = array('type'=>1,'root_id'=>0,'parent_id'=>0,'status'=>1);
        $fields     = array('id','name','g','m','a','param');
        $root_menus = $this->field($fields)->where($where)->order(array('rank'=>'ASC'))->select();
        if(!empty($root_menus))
        {
            foreach($root_menus as $root)
            {
                $root_array = array
                (
                    'id'    => 'menu_'.$root['id'],
                    'name'  => $root['name'],
                    'tip'   => $root['name'],
                    'items' => ''
                );
                $where  = array('root_id'=>$root['id'],'status'=>1);
                $second = $this->field($fields)->where($where)->order(array('rank'=>'ASC'))->select();
                $items  = '';
                if(!empty($second))
                {
                    foreach($second as $item)
                    {
                        // 控制菜单显示
                        $show   = false;
                        if($login_info['auth'] != '*')
                        {
                            $where  = array('parent_id' => $item['id'],'type' => 1);
                            $result = $this->field('id')->where($where)->select();
                            if(!empty($result))
                            {
                                foreach($result as $key)
                                {
                                    if(in_array($key['id'],$login_info['auth']))
                                    {
                                        $show = true;
                                        break;
                                    }
                                }
                            }
                        }
                        else
                        {
                            $show = true;
                        }
                        if($show)
                        {
                            $json_item= array
                            (
                                'id'   => 'node_'.$item['id'],
                                'name' => $item['name'],
                                'tip'  => $item['name'],
                                'url'  => U($item['g'].'/'.$item['m'].'/'.$item['a'],$item['param'])
                            );
                            $items[$json_item['id']]   = $json_item;
                        }
                    }
                }
                $root_array['items'] = $items;
                $menu_json[$root_array['id']] = $root_array;
            }
        }
        // 去除没有可显示菜单的导航
        foreach($menu_json as $key => $val)
        {
            if(empty($val['items']))
            {
                unset($menu_json[$key]);
            }
        }
        return json_encode($menu_json);
    }
    
    /**
     * 系统所有节点分组
     */
    public function getAllSystemNodeGroup($type = 1,$all = true)
    {
        // 一级节点
        $where      = array('type'=>$type,'root_id'=>0,'parent_id'=>0);
        $fields     = array('id','name','g','m','a','param','root_flg','status','rank');
        $roots      = $this->field($fields)->where($where)->order(array('rank'=>'ASC'))->select();
        if(!empty($roots))
        {
            foreach($roots as &$root)
            {
                // 主节点
                $where             = array('type'=>$type,'root_flg'=>1,'root_id'=>$root['id']);
                $root['left_menu'] = $this->field($fields)->where($where)->order(array('rank'=>'ASC'))->select();
                if($all === true)
                {
                    if(!empty($root['left_menu']))
                    {
                        foreach($root['left_menu'] as &$left)
                        {
                            $where              = array('type'=>$type,'parent_id'=>$left['id']);
                            $left['node_items'] = $this->field($fields)->where($where)->order(array('rank'=>'ASC'))->select();
                        }
                    }
                }
            }
        }
        return $roots;
    }
}